package com.thoughtworks.collection;

import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import java.util.*;

public class Add {
    public int getSumOfEvens(int leftBorder, int rightBorder) {
        int max = 0;
        int min = 0;
        int sum = 0;
        if(leftBorder>rightBorder){
             max = leftBorder;
             min = rightBorder;
        }else if(leftBorder<rightBorder){
             max = rightBorder;
             min = leftBorder;
        }
        for (int i = min ; i<= max ;i++){
            if(i%2==0){
                sum+=i;
            }
        }
        return sum;
    }

    public int getSumOfOdds(int leftBorder, int rightBorder) {
        int max = 0;
        int min = 0;
        int sum = 0;
        if(leftBorder>rightBorder){
            max = leftBorder;
            min = rightBorder;
        }else if(leftBorder<rightBorder){
            max = rightBorder;
            min = leftBorder;
        }
        for (int i = min ; i<= max ;i++){
            if(i%2!=0){
                sum+=i;
            }
        }
        return sum;
    }

    public int getSumTripleAndAddTwo(List<Integer> arrayList) {
        int sum = 0;
        for (Integer arr: arrayList) {
            sum+=arr*3+2;
        }
        return sum;
    }

    public List<Integer> getTripleOfOddAndAddTwo(List<Integer> arrayList) {
        List<Integer> list = new ArrayList<>();
        for (Integer arr: arrayList) {
            if(arr%2!=0){
                int newArr=arr*3+2;
                list.add(newArr);
            }else {
                list.add(arr);
            }
        }
        return list;
    }

    public int getSumOfProcessedOdds(List<Integer> arrayList) {
        int sum = 0;
        for (Integer arr: arrayList) {
            if(arr%2!=0){
                sum+=arr*3+5;
            }
        }
        return sum;
    }

    public double getMedianOfEven(List<Integer> arrayList) {
        List<Integer> list = new ArrayList<>();
        double num = 0;
        int a = 1;
        for (Integer i : arrayList) {
            if (i % 2 == 0) {
                list.add(i);
            }
        }
        if (list.size() % 2 == 0) {
            num = (list.get(list.size() / 2 - 1) + list.get(list.size() / 2)) / 2;
        } else {
            num = list.get(list.size() / 2 + 1);
        }

        return num;
    }

    public double getAverageOfEven(List<Integer> arrayList) {
        int i = 0;
        double sum = 0;
        for (Integer arr:arrayList) {
            if(arr%2==0){
                sum+=arr;
                i++;
            }
        }
        return sum/i;
    }

    public boolean isIncludedInEvenIndex(List<Integer> arrayList, Integer specialElment) {
        List<Integer> list = new ArrayList<>();
        for ( Integer arr:arrayList) {
            if (arr%2==0){
                list.add(arr);
            }
        }
        boolean b= false;
        for (Integer l:list) {
            if(l==specialElment){
                 b= true;
            }
        }
        return b;
    }

    public List<Integer> getUnrepeatedFromEvenIndex(List<Integer> arrayList) {
        List<Integer> list = new ArrayList<>();
        for ( Integer arr:arrayList) {
            if (arr%2==0){
                list.add(arr);
            }
        }
        Set<Integer> middleHashSet = new HashSet<Integer>(list);

        List<Integer> afterHashSetList = new ArrayList<Integer>(middleHashSet);
        return  afterHashSetList;
    }

    public List<Integer> sortByEvenAndOdd(List<Integer> arrayList) {
        List<Integer> odd = new ArrayList<Integer>();
        List<Integer> even = new ArrayList<Integer>();

        int temp = 0;
        for (int j=0; j<arrayList.size(); j++) {
            temp= arrayList.get(j);
            if(temp%2==0) {
                even.add(temp);
            }else {
                odd.add(temp);
            }
            Collections.sort(even);
            Collections.sort(odd,Collections.reverseOrder());

            int m = 0;
            for(int i=0; i < even.size(); i++){
                arrayList.set(m, even.get(i));
                m++;
            }
            for(int jj = 0;jj < odd.size();jj++){
                arrayList.set(m, odd.get(jj));
                m++;
            }
        }
        return arrayList;
    }

    public List<Integer> getProcessedList(List<Integer> arrayList) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < arrayList.size() - 1; i++) {
            int r = (arrayList.get(i) + arrayList.get(i + 1)) * 3;
            list.add(r);
        }
        return list;
    }
}
